package com.tushare.controller;

import com.tushare.entity.HsConst;
import com.tushare.entity.StockBasic;
import com.tushare.entity.StockCompany;
import com.tushare.network.WapResponse;
import com.tushare.util.ServiceUtil;
import com.tushare.util.TransformClassUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@CrossOrigin("*")
public class BasicInfoController {
    private static final String TAG = "BasicInfoController";
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final TransformClassUtil transformClassUtil = new TransformClassUtil();
    private final ServiceUtil serviceUtil = new ServiceUtil();
    @Autowired
    RestTemplate restTemplate;

    /**
     * 股票列表
     */
    @GetMapping("stock_basic")
    public List<StockBasic> getIncome() {
        serviceUtil.initWapRequest("stock_basic");
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, StockBasic.class);
    }

    /**
     * 上市公司基本信息
     */
    @GetMapping("stock_company")
    public List<StockCompany> getStockCompany() {
        serviceUtil.initWapRequest("stock_company");
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, StockCompany.class);
    }

    /**
     * 沪深股通成份股
     *
     * @param hsType 类型SH沪股通SZ深股通
     */
    @GetMapping(value = "hs_const", params = {"hs_type"})
    public List<HsConst> getHsConst(@RequestParam("hs_type") String hsType) {
        serviceUtil.initWapRequest("hs_const");
        serviceUtil.appendField("hs_type", hsType);
        WapResponse.WapData data = serviceUtil.getNetworkData(restTemplate);
        return data == null ? null : transformClassUtil.initList(data, HsConst.class);
    }
}
